﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace IP20.Procdure
{
    public class UserHelper
    {
        private static string fileName = "user.info";

        static UserHelper()
        {
#if JQ8931314
            fileName = "user2.info";
#endif
        }

        private static UserHelper instance;

        private List<UserEntity> userList = new List<UserEntity>();

        public static UserHelper GetInstance()
        {
            return instance ?? (instance = new UserHelper());
        }

        //检查登录
        public UserEntity CheckUserInfo(string name, string pwd)
        {
            try
            {
                UserEntity user = (userList.Find(x => x.UserName.ToString() == name));
                if (user == null)
                {
                    return null;
                }

                if (user.Password == pwd)
                {
                    return user;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //获取所有用户
        public List<UserEntity> UserList
        { get { return userList; } }

        private UserHelper()
        {
            CheckFile();
            userList = DeSerializedUser();
        }

        //序列化到文件
        private bool SerializedUser()
        {
            if (userList == null)
            {
                return false;
            }
            BinaryFormatter format = new BinaryFormatter();
            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                format.Serialize(fs, userList);
                return true;
            }
        }

        //反序列化到类
        private List<UserEntity> DeSerializedUser()
        {
            BinaryFormatter format = new BinaryFormatter();
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    object o = format.Deserialize(fs);
                    return o as List<UserEntity>;
                }
            }
            catch
            {
                throw;
            }
        }

        //判断文件是否存在
        private void CheckFile()
        {
            //不存在用户则创建管理员用户
            if (!File.Exists(fileName))
            {
                UserEntity user = new UserEntity()
                {
                    UserName = "admin",
                    Password = "admin",
                    UserLevel = UserLevel.Admin,
                };
                UserEntity user2 = new UserEntity()
                {
                    UserName = "user",
                    Password = "user",
                    UserLevel = UserLevel.Operator,
                };
                userList.Add(user);
                userList.Add(user2);
                SerializedUser();
            }
        }

        //新增用户
        public bool AddUser(UserEntity user)
        {
            if (null != CheckContainUser(user.UserName))
            {
                return false;
            }

            userList.Add(user);
            SerializedUser();
            return true;
        }

        // 检查用户是否存在
        private UserEntity CheckContainUser(string userName)
        {
            return userList.Find(x => x.UserName.ToString() == userName);
        }

        // 删除用户
        public bool DeleteUser(string userName)
        {
            if (null == CheckContainUser(userName))
            {
                return true;
            }

            userList.Remove(CheckContainUser(userName));
            SerializedUser();
            return true;
        }

        // 修改用户信息
        public bool EditUser(UserEntity user)
        {
            if (null == CheckContainUser(user.UserName))
            {
                return false;
            }

            DeleteUser(user.UserName);
            AddUser(user);
            SerializedUser();

            return true;
        }
    }
}